<?php
if(((isset($allowpost) && $allowpost) || $allowupdate) && isset($PostFlagEnabled) && $PostFlagEnabled && !empty($PostFlagsManageData)){
$GLOBALS['load_postflag_style'] = true;
?>
<div class="btn-group stdkit" id="stdkitpfm">
    <button type="button" id="postflagmanage_0" class="btn btn-secondary dropdown-toggle postflagmanagebtn" data-toggle="dropdown"  aria-haspopup="true" aria-expanded="false" title="标记评论">
        标记
    </button>
    <input type="hidden" id="threadmanagepostflag_0" name="threadmanagepostflag_0" value="" />
    <ul class="dropdown-menu">
        <?php
$_curflagroup = -1;
foreach ($PostFlagsManageData as $flagroup => &$_FlagsData) {
        if ($_curflagroup == -1) $_curflagroup = $flagroup;
        if ($_curflagroup != $flagroup) {
        ?>
        <li class="dropdown-divider"></li>
        <?php
        }
        foreach ($_FlagsData as $_FlagDataN => &$_FlagDataV) {
        ?>
        <li class="dropdown-item ghx-postflag-<?php echo $_FlagDataN;?>" data-AdditionClass="<?php echo $_FlagDataV['AdditionClass'];?>" data-Group="<?php echo $_FlagDataV['Group'];?>" data-Unique="<?php echo $_FlagDataV['Unique'];?>" title="<?php echo $_FlagDataV['info'];?>"><a href="javascript:;" target="_self" onclick="javascript:SetPostFlag(this,'<?php echo $_FlagDataN;?>');"><?php echo $_FlagDataV['title'];?></a></li>
        <?php
            }
        $_curflagroup = $flagroup;
  }
        ?>
        <li class="dropdown-divider"></li>
        <li class="dropdown-item ghx-postflag-clear" title="取消标记"><a href="javascript:;" target="_self" onclick="javascript:SetPostFlag(this,'clear');">取消</a></li>
        <li class="dropdown-item ghx-postflag-striphtml" title="去掉HTML格式"><a href="javascript:;" target="_self" onclick="javascript:SetPostFlag(this,'striphtml');">文本化</a></li>
    </ul>
</div>
<script type="text/javascript">
    if(typeof(PostFlagList) == 'undefined')
        var PostFlagList = <?php echo xn_json_encode($PostFlagCfg['Flags']);?>;
    var CurPostFlagList = PostFlagList;
    var postlistcount = <?php echo count($postlist);?>;
    /**
     * 生成帖子标记按钮
     * @param postflag_cfg {array} 标记配置表,缺省值为PostFlagList，如果配置为空，不执行
     */
    function generate_postflag_manage_hander(postflag_cfg) {
        if (typeof (postflag_cfg) == 'undefined' && typeof (PostFlagList) != 'undefined')
            postflag_cfg = PostFlagList;
        var succs = 0;
        if (typeof(postflag_cfg) == 'object' && count(postflag_cfg) > 0) {
            if(CurPostFlagList != postflag_cfg) CurPostFlagList = postflag_cfg;
            var PostflagSentry = $('.card-postlist .postlist .post .media-body A.post_reply');
            if (PostflagSentry.length == 0) PostflagSentry = $('.card-postlist .postlist .post .media-body A.post_delete');
            if (PostflagSentry.length > 0) {
                var ele_stdkitpfm = $('#stdkitpfm');
                if (ele_stdkitpfm.length == 1) {
                    PostflagSentry.each(function (index, element) {
                        var my_stdkitpfm = ele_stdkitpfm.clone(true);
                        my_stdkitpfm.removeAttr('id').removeClass('stdkit');
                        var pid = $(element).data('pid');
                        var tid = null;
                        if (!pid) {
                            pid = $(element).parents('.postlist > .post').data('pid');
                        } else {
                            tid = $(element).data('tid');
                        }
                        if (pid) {
                            var optHander = $('#postflagmanage_0', my_stdkitpfm[0]);
                            optHander.attr('id', 'postflagmanage_' + pid).data('pid', pid);
                            if (tid > 0) {
                                optHander.data('tid', tid);
                            }
                            $('#threadmanagepostflag_0', my_stdkitpfm[0]).attr('name', 'threadmanagepostflag_' + pid).attr('id', 'threadmanagepostflag_' + pid);
                            $(element).before(my_stdkitpfm);
                            succs++;
                        }
                    });
                }
            }
        }
        return succs;
    }

    /**
     * 生成主题贴标记按钮
     * @returns {number}
     */
    function generate_firstpostflag_manage_btn(){
        var succs = 0;
        if (typeof(PostFlagList) == 'object' && count(PostFlagList) > 0) {
            var PostflagSentry = $('.card-thread #thread_marker');
            if(PostflagSentry.length == 1){
                var ele_stdkitpfm = $('#stdkitpfm');
                if (ele_stdkitpfm.length == 1){
                    var my_stdkitpfm = ele_stdkitpfm.clone(true);
                    my_stdkitpfm.removeAttr('id').removeClass('stdkit');
                    var pid = PostflagSentry.data('pid');
                    var tid = PostflagSentry.data('tid');
                    var uid = PostflagSentry.data('uid');
                    if (pid) {
                        var optHander = $('#postflagmanage_0', my_stdkitpfm);
                        optHander.attr('id', 'postflagmanage_' + pid).data('pid', pid);
                        if (tid > 0) {
                            optHander.data('tid', tid);
                        }
                        optHander.data('uid', uid).data('type','first');
                        $('#threadmanagepostflag_0', my_stdkitpfm).attr('name', 'threadmanagepostflag_' + pid).attr('id', 'threadmanagepostflag_' + pid);
                        PostflagSentry.after(my_stdkitpfm);
                        succs++;
                    }
                }
            }
        }
        return succs;
    }
</script>
<?php
   unset($_curflagroup, $_FlagsData, $_FlagDataV, $flagroup, $_FlagsData);
 }
?>